package org.xukai.rest.common;

import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

/**
 * @author xukai
 * @desc
 * @date 2016-12-29-下午 6:10
 */
public enum RespCodeType {

    /**
     * 通用       [000000 - 099999]
     */
    COMMON(0, "通用"),
    /**
     * 限制       [100000 - 199999]
     */
    LIMIT(1, "限制"),
    /**
     * 非法参数    [200000 - 299999]
     */
    INVALID(2, "非法"),
    /**
     * 参数失效    [300000 - 399999]
     */
    EXPIRE(3, "失效");

    /**
     * 间距
     */
    public static final int STEP = 100000;

    /**
     * 存放CODE -> Enum的Map
     */
    private static final ConcurrentMap<Integer, RespCodeType> CODE_MAP = new ConcurrentHashMap<Integer, RespCodeType>(
            RespCodeType.values().length);

    /**
     * 填充CODE --> Enum的Map
     */
    static {
        for (RespCodeType respCodeType : RespCodeType.values()) {
            CODE_MAP.put(respCodeType.code, respCodeType);
        }
    }

    /**
     * 编码
     */
    private final int code;
    /**
     * 最小值
     * code * GAP
     */
    private final int min;
    /**
     * 最大值
     * code * GAP + GAP - 1
     */
    private final int max;
    /**
     * 备注
     */
    private final String note;

    RespCodeType(int code, String note) {
        this.code = code;
        this.min = code * STEP;
        this.max = code * STEP + STEP - 1;
        this.note = note;
    }

    public static RespCodeType fromCode(Integer code) {
        return CODE_MAP.get(code);
    }

    @Override
    public String toString() {
        return "ResponseCodeType{" +
                "code=" + code +
                ", min=" + min +
                ", max=" + max +
                ", note='" + note + '\'' +
                '}';
    }
}
